package com.gouaixin.bee.beeframework2.dao;

import android.content.Context;
import android.util.Log;


import com.gouaixin.bee.beeframework2.tool.mLog;
import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.bean.ZhenxinhuaFriend;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/4/7.
 */
//真心话好友数据库操作器
public class zhenxinhuaFriendDao {

    public static void add(Context context, ZhenxinhuaFriend bean){

        mLog.e("进入添加真心话好友的方法：bean=" + new Gson().toJson(bean));

        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);

            //首先判断数据库是否已经存在当前对象
            List<ZhenxinhuaFriend> list =new ArrayList<ZhenxinhuaFriend>();

            String jid=bean.getJid();
            mLog.e("添加真心话好友:bean=" + new Gson().toJson(bean));


                    list = db.findAll(Selector.from(ZhenxinhuaFriend.class).where(WhereBuilder.b("jid", "=", jid)));

//                Log.e("db",new Gson().toJson(list));
//                Log.e("db","lsit.size()="+list.size());

                if(list==null||list.size()==0){
                    db.saveBindingId(bean);
                    String cid= PreferencesUtils.getString(context, "cid");
                    int ZhenxinhuaFriendNumer= PreferencesUtils.getInt(context, cid + "ZhenxinhuaFriend", 0);
                    ZhenxinhuaFriendNumer++;
                    PreferencesUtils.putInt(context, cid+"ZhenxinhuaFriend",ZhenxinhuaFriendNumer);
                    int newZhenxinhuaFriendNumer= PreferencesUtils.getInt(context, cid + "ZhenxinhuaFriend", 0);
                    mLog.e("添加真心话好友的方法（控制器中）：newZhenxinhuaFriendNumer=" + newZhenxinhuaFriendNumer);
                }
        } catch (DbException e) {
            mLog.e("添加真心话好友的方法：错误=" + e.getMessage());

        }
    }





    public static List<ZhenxinhuaFriend> query(Context context){
        List<ZhenxinhuaFriend> list =new ArrayList<ZhenxinhuaFriend>();

        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);


             list = db.findAll(Selector.from(ZhenxinhuaFriend.class));//.where(WhereBuilder.b("name", "=", "child' name")));
            mLog.e("查询真心话好友：list=" + new Gson().toJson(list));

        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }

        return list;
    }




}
